import download from "download-git-repo";
import ora from "ora";
import chalk from "chalk";
import shell from "shelljs"
import fs from "fs-extra";
import logSymbols from "log-symbols";
import { removeDir, getDirPath } from './utils.js'

// 更新模板
export default async function updateTemplate(appName) {
  if (!shell.which("git")) {
    console.log(logSymbols.error, chalk.redBright("Error：运行脚手架必须先安装git！"));
    shell.exit(1);
  }
  const filePath = getDirPath('template')
  if (fs.existsSync(filePath)) {
    await removeDir(filePath)
  }
  const spinner = ora("正在获取模板......").start();
  return new Promise((resolve, reject) => {
    // 第四个参数为一个callback，如果err存在则代表拉取项目失败
    download(
      'direct:https://gitee.com/cixun1998/cx-web-template.git',
      filePath,
      {clone: true},
      err => {
      if (err) {
        spinner.fail(chalk.red(err));
        reject(err);
        return;
      }
      spinner.succeed(chalk.greenBright("获取模板成功"));
      resolve();
    })
  })
}
